home *** CD-ROM | disk | FTP | other *** search
- unit DCRichEditReg;
-
- {$ifdef Ver93} { C++ Builder 1.0x }
- {$define DelphiLessThan4}
- {$endif}
- {$ifdef Ver100} { Delphi 3.0x }
- {$define DelphiLessThan4}
- {$endif}
- {$ifdef Ver110} { C++ Builder 3.0x }
- {$define DelphiLessThan4}
- {$endif}
-
- interface
-
- uses
- {$ifndef DelphiLessThan4}
- ImgList,
- {$endif}
- Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
- StdCtrls, ComCtrls, DCRichEdit, ExtCtrls, ToolWin;
-
- type
- TRTFForm = class(TForm)
- RichEdit: TRichEdit;
- OKButton: TButton;
- CancelButton: TButton;
- OpenDialog: TOpenDialog;
- Timer: TTimer;
- StandardToolBar: TToolBar;
- ClearButton1: TToolButton;
- OpenButton: TToolButton;
- ToolButton5: TToolButton;
- CutButton: TToolButton;
- CopyButton: TToolButton;
- PasteButton: TToolButton;
- UndoButton: TToolButton;
- ToolButton10: TToolButton;
- FontName: TComboBox;
- ToolButton11: TToolButton;
- FontSize: TEdit;
- UpDown1: TUpDown;
- ToolButton2: TToolButton;
- BoldButton: TToolButton;
- ItalicButton: TToolButton;
- UnderlineButton: TToolButton;
- ToolButton16: TToolButton;
- LeftAlign: TToolButton;
- CenterAlign: TToolButton;
- RightAlign: TToolButton;
- ToolButton20: TToolButton;
- BulletsButton: TToolButton;
- ToolbarImages: TImageList;
- ToolButton1: TToolButton;
- ColorButton: TToolButton;
- ColorDialog: TColorDialog;
- procedure FormCreate(Sender: TObject);
- procedure FormKeyDown(Sender: TObject; var Key: Word;
- Shift: TShiftState);
- procedure RichEditSelectionChange(Sender: TObject);
- procedure ClearButton1Click(Sender: TObject);
- procedure OpenButtonClick(Sender: TObject);
- procedure CutButtonClick(Sender: TObject);
- procedure CopyButtonClick(Sender: TObject);
- procedure PasteButtonClick(Sender: TObject);
- procedure UndoButtonClick(Sender: TObject);
- procedure FontNameChange(Sender: TObject);
- procedure FontSizeChange(Sender: TObject);
- procedure BoldButtonClick(Sender: TObject);
- procedure ItalicButtonClick(Sender: TObject);
- procedure UnderlineButtonClick(Sender: TObject);
- procedure AlignClick(Sender: TObject);
- procedure BulletsButtonClick(Sender: TObject);
- procedure TimerTick(Sender: TObject);
- procedure ColorButtonClick(Sender: TObject);
- private
- FUpdating: Boolean;
- procedure GetFontNames;
- function CurrText: TTextAttributes;
- end;
-
- procedure Register;
-
- implementation
-
- {$R *.DFM}
-
- uses
- DsgnIntf, ClipBrd, ActiveX;
-
- //Property editor
- type
- TLinesProperty = class(TClassProperty)
- public
- procedure Edit; override;
- function GetAttributes: TPropertyAttributes; override;
- end;
-
- { TLinesProperty }
-
- procedure TLinesProperty.Edit;
- var
- Loop: Integer;
- NewRTF: String;
- begin
- with TRTFForm.Create(Application) do
- try
- StringToRichEditLines(
- RichEditLinesToString(GetComponent(0) as TCustomRichEdit), RichEdit);
- //Display RTF editing form
- if ShowModal = mrOk then
- begin
- //If user presses OK, give new property value to selected components
- NewRTF := RichEditLinesToString(RichEdit);
- for Loop := 0 to PropCount - 1 do
- StringToRichEditLines(NewRTF, GetComponent(Loop) as TCustomRichEdit);
- //Make sure form designer spots the property change
- Self.Designer.Modified
- end
- finally
- Free
- end
- end;
-
- function TLinesProperty.GetAttributes: TPropertyAttributes;
- begin
- //Request the ellipsis button, and remove the expandable property capability
- Result := inherited GetAttributes + [paDialog] - [paSubProperties]
- end;
-
- //Component editor
- type
- TDCRichEditEditor = class(TComponentEditor)
- public
- procedure ExecuteVerb(Index: Integer); override;
- function GetVerb(Index: Integer): string; override;
- function GetVerbCount: Integer; override;
- end;
-
- { TDCRichEditEditor }
-
- procedure TDCRichEditEditor.ExecuteVerb(Index: Integer);
- begin
- if Index = 0 then
- with TRTFForm.Create(Application) do
- try
- StringToRichEditLines(
- RichEditLinesToString(Component as TDCRichEdit), RichEdit);
- //Display RTF loading form and proceed if OK is pressed
- if ShowModal = mrOk then
- begin
- StringToRichEditLines(
- RichEditLinesToString(RichEdit), Component as TDCRichEdit);
- //Make sure form designer spots the property change
- Self.Designer.Modified
- end
- finally
- Free
- end
- end;
-
- function TDCRichEditEditor.GetVerb(Index: Integer): string;
- begin
- if Index = 0 then
- Result := 'Edit formatted text...'
- end;
-
- function TDCRichEditEditor.GetVerbCount: Integer;
- begin
- Result := 1
- end;
-
- procedure Register;
- begin
- RegisterComponents('Clinic', [TDCRichEdit]);
- RegisterPropertyEditor(TypeInfo(TStrings), TDCRichEdit,
- 'Lines', TLinesProperty);
- RegisterComponentEditor(TDCRichEdit, TDCRichEditEditor)
- end;
-
- { TRTFForm } //The RTF editing form
-
- function EnumFontsProc(var LogFont: TLogFont; var TextMetric: TTextMetric;
- FontType: Integer; Data: Pointer): Integer; stdcall;
- begin
- TStrings(Data).Add(LogFont.lfFaceName);
- Result := 1;
- end;
-
- procedure TRTFForm.GetFontNames;
- var
- DC: HDC;
- begin
- DC := GetDC(0);
- try
- EnumFonts(DC, nil, @EnumFontsProc, Pointer(FontName.Items))
- finally
- ReleaseDC(0, DC)
- end;
- FontName.Sorted := True;
- end;
-
- function TRTFForm.CurrText: TTextAttributes;
- begin
- if RichEdit.SelLength > 0 then
- Result := RichEdit.SelAttributes
- else
- Result := RichEdit.DefAttributes;
- end;
-
- procedure TRTFForm.FormCreate(Sender: TObject);
- begin
- GetFontNames;
- RichEditSelectionChange(RichEdit);
- end;
-
- procedure TRTFForm.FormKeyDown(Sender: TObject; var Key: Word;
- Shift: TShiftState);
- begin
- if Key = vk_Escape then
- CancelButton.Click
- end;
-
- var
- CF_RTF: TClipFormat;
-
- procedure TRTFForm.RichEditSelectionChange(Sender: TObject);
- begin
- with RichEdit.Paragraph do
- try
- FUpdating := True;
- CutButton.Enabled := RichEdit.SelLength > 0;
- CopyButton.Enabled := CutButton.Enabled;
- BoldButton.Down := fsBold in RichEdit.SelAttributes.Style;
- ItalicButton.Down := fsItalic in RichEdit.SelAttributes.Style;
- UnderlineButton.Down := fsUnderline in RichEdit.SelAttributes.Style;
- BulletsButton.Down := Boolean(Numbering);
- FontSize.Text := IntToStr(RichEdit.SelAttributes.Size);
- FontName.Text := RichEdit.SelAttributes.Name;
- case Ord(Alignment) of
- 0: LeftAlign.Down := True;
- 1: RightAlign.Down := True;
- 2: CenterAlign.Down := True;
- end;
- finally
- FUpdating := False;
- end;
- end;
-
- procedure TRTFForm.ClearButton1Click(Sender: TObject);
- begin
- RichEdit.Lines.Clear
- end;
-
- procedure TRTFForm.OpenButtonClick(Sender: TObject);
- begin
- if OpenDialog.Execute then
- RichEdit.Lines.LoadFromFile(OpenDialog.FileName);
- end;
-
- procedure TRTFForm.CutButtonClick(Sender: TObject);
- begin
- RichEdit.CutToClipboard
- end;
-
- procedure TRTFForm.CopyButtonClick(Sender: TObject);
- begin
- RichEdit.CopyToClipboard
- end;
-
- procedure TRTFForm.PasteButtonClick(Sender: TObject);
- begin
- RichEdit.PasteFromClipboard
- end;
-
- procedure TRTFForm.UndoButtonClick(Sender: TObject);
- begin
- //Delphi 4 introduces an Undo method to replace this
- RichEdit.Perform(WM_UNDO, 0, 0)
- end;
-
- procedure TRTFForm.FontNameChange(Sender: TObject);
- begin
- if not FUpdating then
- CurrText.Name := FontName.Items[FontName.ItemIndex];
- end;
-
- procedure TRTFForm.FontSizeChange(Sender: TObject);
- begin
- if not FUpdating then
- CurrText.Size := StrToInt(FontSize.Text);
- end;
-
- procedure TRTFForm.BoldButtonClick(Sender: TObject);
- begin
- if not FUpdating then
- if BoldButton.Down then
- CurrText.Style := CurrText.Style + [fsBold]
- else
- CurrText.Style := CurrText.Style - [fsBold];
- end;
-
- procedure TRTFForm.ItalicButtonClick(Sender: TObject);
- begin
- if not FUpdating then
- if ItalicButton.Down then
- CurrText.Style := CurrText.Style + [fsItalic]
- else
- CurrText.Style := CurrText.Style - [fsItalic];
- end;
-
- procedure TRTFForm.UnderlineButtonClick(Sender: TObject);
- begin
- if not FUpdating then
- if UnderlineButton.Down then
- CurrText.Style := CurrText.Style + [fsUnderline]
- else
- CurrText.Style := CurrText.Style - [fsUnderline];
- end;
-
- procedure TRTFForm.AlignClick(Sender: TObject);
- begin
- if not FUpdating then
- RichEdit.Paragraph.Alignment := TAlignment(TControl(Sender).Tag);
- end;
-
- procedure TRTFForm.BulletsButtonClick(Sender: TObject);
- begin
- if not FUpdating then
- RichEdit.Paragraph.Numbering := TNumberingStyle(BulletsButton.Down);
- end;
-
- procedure TRTFForm.TimerTick(Sender: TObject);
- begin
- PasteButton.Enabled :=
- ClipBoard.HasFormat(CF_TEXT) or
- ClipBoard.HasFormat(CF_OEMTEXT) or
- ClipBoard.HasFormat(CF_UNICODETEXT) or
- ClipBoard.HasFormat(CF_RTF);
- end;
-
- procedure TRTFForm.ColorButtonClick(Sender: TObject);
- begin
- if FUpdating then
- Exit;
- ColorDialog.Color := CurrText.Color;
- if ColorDialog.Execute then
- CurrText.Color := ColorDialog.Color
- end;
-
- initialization
- CF_RTF := RegisterClipboardFormat('Rich Text Format');
- end.
-